<template>
  <d2-container>
    <!-- 楼宇管理系统主页面-->
    <el-table :data="tableData" style="width: 100%" stripe size="mini" v-loading="loading">
      <el-table-column prop="buildingName" label="楼宇名称"></el-table-column>
      <el-table-column prop="buildingManager" label="管理员"></el-table-column>
      <el-table-column prop="buildingManagerTel" label="管理员电话"></el-table-column>
      <el-table-column prop="buildingFloor" label="总楼层"></el-table-column>
      <el-table-column prop="areaName" label="小区名称"></el-table-column>

      <!-- 右侧按钮 -->
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="onAdd">新增</el-button>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="success" plain @click="onUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <template slot="footer">
      <el-pagination background layout="prev, pager, next" :total="1000"></el-pagination>
    </template>

    <!-- 弹出框 -->
    <el-dialog title="提示" :visible.sync="showModify" width="30%" center>
      <el-form ref="form" :model="formData" label-width="100px" :rules="validateRules">
        <el-form-item label="小区名称" prop="areaName">
          <el-input
            v-model="formData.groupName"
            placeholder="请输入小区名称"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item label="管理员" prop="buildingManager">
          <el-input
            v-model="formData.buildingManager"
            placeholder="请输入管理员"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item label="管理员电话" prop="buildingManagerTel">
          <el-input
            v-model="formData.buildingManagerTel"
            placeholder="请输入管理员电话"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item label="总楼层" prop="buildingFloor">
          <el-input-number
            v-model="formData.buildingFloor"
            controls-position="right"
            @change="handleChange"
            :min="-1"
            :max="50"
          ></el-input-number>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onModify">确定</el-button>
          <el-button @click="showModify=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </d2-container>
</template>

<script>
import { mapActions } from "vuex";

export default {
  mounted() {
    this.loading = true;
    this.getBuildingList().then(result => {
      this.tableData = result;
      this.loading = false;
    });
  },
  data() {
    return {
      loading: false,
      formData: {},
      showModify: false,
      tableData: [],
      //下拉框数据
      options: [
        {
          value: "选项1",
          label: "11"
        },
        {
          value: "选项2",
          label: "22"
        },
        {
          value: "选项3",
          label: "33"
        }
      ],
      value: "",

      validateRules: {
        areaName: [
          { required: true, message: "请输入小区名称", trigger: "blur" }
        ],
        buildingManager: [
          { required: true, message: "请选择管理员", trigger: "blur" }
        ],
        buildingManagerTel: [
          { required: true, message: "请输入管理员电话", trigger: "blur" }
        ],
        buildingFloor: [
          { required: true, message: "请选择总楼层", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    ...mapActions("pms/base.building", ["getBuildingList"]),
    handleChange(value) {
      console.log(value);
    },
    onAdd() {
      this.showModify = true;
    },
    onUpdate(row) {
      console.log(row);
      this.formData = row;
      this.showModify = true;
    },
    onModify() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formData.id) {
            console.log("这是修改方法,调用修改方法");
          } else {
            console.log("这是新增方法");
          }
          this.showModify = false;
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>